package com.power.algorithm.match;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/5/28 10:45 上午
 */
public class NativeStringMatcher {
    public static void main(String[] args) {
        String origin = "abcdegabdx";
        String child = "abdx";
        NativeStringMatcher nsm = new NativeStringMatcher();
        int index = nsm.match(origin, child);
        System.out.println("有效位移是： " + index);

    }


    public int match(String origin, String child) {
        int lOrigin = origin.length();
        int lChild = child.length();
        for (int i = 0; i < lOrigin; i++) {
            int k = i;
            for (int j = 0; i < lChild; j++) {
                if (origin.charAt(k) != child.charAt(j)) {
                    break;
                } else {
                    k++;
                    if (j == lChild - 1) {
                        return i;
                    }
                }
            }
        }
        return -1;
    }


}
